$(function(){
    // find all the input elements with title attributes
    $('input[title!=""]').hint();
});

///// MEMBER LOGIN \\\\\\\
$(function(){
    $('#btnlogin').click(function(){
        var username = $('#txtusername').val();
        var password = $('#txtpassword').val();
        
        $.post("library/ajaxsubmit.php",{
            form: 'login',
            txtusername: username,
            txtpassword: password
        },function(data){
            var result  = data;
            var arr     = result.split('~');
            if(arr[1]=='success'){
                location.href="index.php";
            }else if(arr[1] == 'error'){
                alert(arr[0])
            }
            $('html, body').animate({scrollTop:0}, 'slow');
        });

    });
});


/////// Category Change \\\\\\\\
$(function(){
    $('#cboCategory').change(function(){
        var cat_id = $(this).val();
        $('#subcat_container').html('loading...');
        $.post("library/ajaxsubmit.php",{
            form: "product_post",
            cat_id: cat_id
        },function(data){
            $('#subcat_container').html(data+' <font color="#ff0000">*</font> required field');
        })
    });

});


//****** upload Step Start *********\\
$(function(){
    
    $('#btnupload').click(function(){
        var title= $('#txtadtitle').val();
        var category = $('#cboCategory').val();
        var subcategory = $('#cboSubCategory').val();
        var price = $('#txtprice').val();
        var adtype= $('#rdoadtype').val();
        
        var desc = CKEDITOR.instances.desc.getData();
        

        if(title=='' || category == '' || desc == ''){
            $('#msg').html("<div class='error_msg'><p>Please type required field(s)</p></div>");
            $('html, body').animate({scrollTop:0}, 'slow');
        }else{
            $.post("library/ajaxsubmit.php",{

                form: 'product_post_insert',
                txtadtitle: title,
                cboCategory: category,
                cboSubCategory: subcategory,
                txtprice: price,
                txtdesc: desc,
                rdoadtype: adtype

            },function(data){
                var result  = data;
                var arr     = result.split('~');
                if(arr[1]=='success'){
                    location.href = arr[0];
                    /*$('#msg').html('<div class="success_msg"><p>'+arr[0]+'</p></div>');
                    $(':input','#frmregister')
                     .not(':button, :submit, :reset, :hidden')
                     .val('')
                     .removeAttr('checked')
                     .removeAttr('selected');*/
                }else{
                    $('#msg').html('<div class="error_msg"><p>You are not able to upload at the moment. Please try again later.</p></div>');
                }
                $('html, body').animate({scrollTop:0}, 'slow');
            });
        }
    });

});



$(function(){
    $('#btncomment').click(function(){
        
        var comment = $('#txtcomment').val();
        var product = $('#hdnproductid').val();
        $.post("library/ajaxsubmit.php",{
            form: 'comment',
            txtcomment: comment,
            product_id: product
        },function(data){
            //commentbody
            var result  = data;
            var arr     = result.split('~');
            if(arr[1]=='success'){
                $('.commentbody').remove();
                $('#commentbody').html(arr[0]);
            }else if(arr[1]=='expire'){
                location.href="index.php"
            }else{
                $('#msg').html('<div class="error_msg"><p>You are not able to upload at the moment. Please try again later.</p></div>');
            }
            //$('html, body').animate({scrollTop:0}, 'slow');
        })
    });
})



